import com.jdbc.AccountDao;
import com.jdbc.Account;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestJDBC01 {
    @Test
    public void testUpdate_add() {
        //初始化spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config01.xml");
        //从spring容器中获取AccountDao实例
        AccountDao dao = context.getBean(AccountDao.class); //设置要添加的数据
        Account account = new Account();
        account.setUsername("Tom");
        account.setBalance(1000.00);
        //调用dao的方法添加记录
        int row = dao.addAccount(account);
        if (row > 0) {
            System.out.println(row + "条记录插入成功！");
        } else {
            System.out.println("插入记录失败！");
        }
    }

    @Test
    public void testUpdate_update() {
        //初始化spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config01.xml");
        //从spring容器中获取AccountDao实例
        AccountDao dao = context.getBean(AccountDao.class); //设置要修改的数据
        Account account = new Account();
        account.setId(1);
        account.setUsername("Tom");
        account.setBalance(1500.00);
        //调用dao的updateAccountById方法修改记录
        int row = dao.updateAccountById(account);
        if (row > 0) {
            System.out.println(row + "条记录修改成功！");
        } else {
            System.out.println("修改记录失败！");
        }
        System.out.println("==============================");
        //调用dao的updateBalanceById方法修改记录
        row = dao.updateBalanceById(2, 2680.00);
        if (row > 0) {
            System.out.println(row + "条记录修改成功！");
        } else {
            System.out.println("修改记录失败！");
        }
    }

    @Test
    public void testUpdate_delete() {
        //初始化spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config01.xml");
        //从spring容器中获取AccountDao实例
        AccountDao dao = context.getBean(AccountDao.class);
        //调用dao的方法删除记录
        int row = dao.deleteAccountById(2);
        if (row > 0) {
            System.out.println(row + "条记录删除成功！");
        } else {
            System.out.println("删除记录失败！");
        }
    }
}